<template>
  <view>{{ countTime }}</view>
</template>

<script>
import { secondCountDown } from '@/utils/common'
export default {
  name: 'BySecond',
  props: {
    time: {
      type: Number,
      default: 0
    }
  },
  data() {
    return {
      countTime: '00:00:00',
      timerBox: null
    }
  },
  watch: {
    time() {
      clearInterval(this.timerBox)
      this.count()
    }
  },
  async mounted() {
    this.count()
  },
  beforeDestroy() {
    if (this.timerBox) {
      clearInterval(this.timerBox)
    }
  },
  methods: {
    async count() {
      let result
      let time = this.time
      this.timerBox = setInterval(() => {
        if (time < 0) {
          clearInterval(this.timerBox)
          this.countTime = '00:00:00'
          this.$emit('timeEnd')
          return
        }
        result = secondCountDown(time)
        this.countTime = result
        time--
      }, 1000)
    }
  }
}
</script>

<style scoped>

</style>
